##### R code for Fig. 1 of lecture 12 ####

myfunc<-function(x) 2*(x+2)*(x-1)
dmyfunc<-function(x) 4*x+2

curve(2*(x+2)*(x-1),from=0.5,to=2.5,ylab='f(x)',xlab='x',xlim=c(.6,2.25),ylim=c(-1.5,8.5))
abline(h=0,col='grey70')
segments(2,0,2,myfunc(2),lty=2)
segments(2,myfunc(2),2-myfunc(2)/dmyfunc(2),0,lty=2)
segments(1.2,0,1.2,myfunc(1.2),lty=2)
points(2,0,pch=16,cex=.7)
points(1.2,0,pch=16,cex=.7)
text(2,0,expression(x[0]),pos=1,cex=.9)
text(1.2,0,expression(x[1]),pos=1,cex=.9)

points(2,myfunc(2),pch=16,cex=.7,col=2)
text(2,myfunc(2),expression(group("(",list(x[0],f(x[0])),")")),pos=2)

slope.func<-function(x) myfunc(2)+dmyfunc(2)*(x-2)
arrows(1.5,slope.func(1.5),1.1,4,code=1,length=.09,angle=45,col=4)
text(1.1,4,expression("slope"=="f "*minute (x[0])),cex=.9,pos=3,col=4)